﻿using System;
using System.Drawing;
using System.Windows.Forms;

using FluentBinding.Native;
using FluentBinding.Testing;
using FluentBinding.WinForms.Domain;

namespace FluentBinding.WinForms
{
    public abstract class BindingTestBase<C> : UnitTest<Bindings<Customer>>
        where C : Control, new()
    {
        #region Protected properties

        protected Customer Customer { get; private set; }

        protected Form Form { get; private set; }
        protected TextBox TextBox1 { get; private set; }
        protected Label Label1 { get; private set; }
        protected Control FocusedControl { get; private set; }

        protected C TargetControl { get; private set; }

        protected Countries Countries { get; private set; }

        #endregion

        #region Protected methods

        protected override Bindings<Customer> CreateTarget()
        {
            Countries = new Countries();

            Customer = new Customer {Name = "Alex"};
            Customer.Country = Countries.Australia;
            Customer.Status = CustomerStatus.Active;

            Customer.IsPremier = true;

            Form = new Form();
            TextBox1 = new TextBox {Location = new Point(50, 100)};
            Label1 = new Label();
            FocusedControl = new Control();

            TargetControl = new C();

            Form.Controls.Add(TargetControl);
            Form.Controls.Add(TextBox1);
            Form.Controls.Add(Label1);
            Form.Controls.Add(FocusedControl);

            return new Bindings<Customer>(Customer);
        }

        protected void InitializeForm()
        {
            Form.Show();
            Form.Hide();
        }

        protected void UpdateControlAndLoseFocus(Action<C> updateAction)
        {
            Form.Show();

            User32Api.ShowWindow(Form.Handle, (int)ShowWindowMode.SW_RESTORE);
            User32Api.SetForegroundWindow(Form.Handle);

            TargetControl.Focus();
            updateAction(TargetControl);
            FocusedControl.Focus();
            Form.Hide();
        }

        protected void UpdateControl(Action<C> updateAction)
        {
            Form.Show();            
            updateAction(TargetControl);
            Form.Hide();
        }

        #endregion
    }
}